//
//  YPLibsManager.swift
//  YPSwift
//
//  Created by ogawa on 2020/4/28.
//  Copyright © 2020 ogawa. All rights reserved.
//

import Foundation
@_exported import RxSwift
@_exported import RxCocoa
@_exported import SnapKit
@_exported import Kingfisher
@_exported import URLNavigator
@_exported import CocoaLumberjack
@_exported import IQKeyboardManagerSwift
@_exported import NVActivityIndicatorView
@_exported import Toast_Swift
@_exported import NSObject_Rx
@_exported import RxViewController
@_exported import RxOptional
@_exported import RxGesture
@_exported import SwifterSwift
@_exported import SwiftDate

class YPLibsManager: NSObject {
    
   static let shared = YPLibsManager()

   override init() {
     super.init()
   }
    
   func setupLibs(with window: UIWindow? = nil) {
        let libsManager = YPLibsManager.shared
        libsManager.setupCocoaLumberjack()
        libsManager.setupKeyboardManager()
        libsManager.setupTheme()
    }
    
    ///主题设置
    func setupTheme() {
        themeService.rx
            .bind({ $0.statusBarStyle }, to: UIApplication.shared.rx.statusBarStyle)
            .disposed(by: rx.disposeBag)
    }
    
    ///日志设置
    func setupCocoaLumberjack() {
        DDLog.add(DDTTYLogger.sharedInstance) // TTY = Xcode console
        DDLog.add(DDOSLogger.sharedInstance)  // ASL = Apple System Logs

        let fileLogger: DDFileLogger = DDFileLogger() // File Logger
        fileLogger.rollingFrequency = TimeInterval(60*60*24)  // 24 hours
        fileLogger.logFileManager.maximumNumberOfLogFiles = 7
        DDLog.add(fileLogger)
    }
    
    ///键盘设置
    func setupKeyboardManager() {
        IQKeyboardManager.shared.enable = true
        IQKeyboardManager.shared.shouldResignOnTouchOutside = true
    }
    
    ///Kingfisher设置
    func setupKingfisher() {
        ImageDownloader.default.downloadTimeout = 15.0 // 15 sec
    }
    
}

extension YPLibsManager {

    func removeKingfisherCache() -> Observable<Void> {
        return ImageCache.default.rx.clearCache()
    }

    func kingfisherCacheSize() -> Observable<Int> {
        return ImageCache.default.rx.retrieveCacheSize()
    }
    
}

